package com.yami.shop.task;

import com.yami.shop.service.SimilarityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Set;
// 每天凌晨3点执行

public class scheduleSimilarity {
    @Autowired
    private SimilarityService similarityService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Scheduled(cron = "0 0 3 * * ?") // 每天凌晨3点执行

    public void scheduleSimilarityUpdate() {
        Set<String> userKeys = redisTemplate.keys("user:*:items");
        userKeys.stream()
                .map(key -> Long.parseLong(key.split(":")[1]))
                .forEach(userId -> similarityService.cacheUserSimilarities(userId, 50)); // 缓存Top-50相似用户
    }
}
